﻿using AutoMapper;
using Comm100.Application.Services;
using Comm100.Framework.Auditing;
using Comm100.Framework.Authorization;
using Comm100.Public.Audit;
using Bot.Application.Dto;
using Bot.Application.Services;
using Bot.Domain.Bo;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;
using Bot.Domain;

namespace Bot.Application.SmartTriggers.Service
{
    public class SmartTriggerAppService : BaseAppService, ISmartTriggerAppService
    {
        private ISmartTriggerDomainService _domainService;

        public SmartTriggerAppService(ISmartTriggerDomainService domainService) : base()
        {
            this._domainService = domainService;
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        [Audit(BotEntity.SMARTTRIGGER, AuditAction.DESTROY)]
        public void Delete(Guid id)
        {
            _domainService.Delete(id);
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        [Audit(BotEntity.SMARTTRIGGER, AuditAction.CREATE)]
        public SmartTriggerDto Add(Guid botId, SmartTriggerCreateDto dto)
        {
            dto.Id = Guid.NewGuid();
            dto.BotId = botId;
            SmartTrigger smartTrigger = _domainService.Create(Mapper.Map<SmartTrigger>(dto));
            return Mapper.Map<SmartTriggerDto>(smartTrigger);
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        [Audit(BotEntity.SMARTTRIGGER, AuditAction.UPDATE)]
        public SmartTriggerDto Update(Guid id, SmartTriggerUpdateDto dto)
        {
            dto.Id = id;
            SmartTrigger smartTrigger = _domainService.Update(Mapper.Map<SmartTriggerUpdateBo>(dto));
            return Mapper.Map<SmartTriggerDto>(smartTrigger);
        }

        public SmartTriggerDto Get(Guid id)
        {
            SmartTrigger smartTrigger = _domainService.GetbyId(id);
            return Mapper.Map<SmartTriggerDto>(smartTrigger);
        }

        public IEnumerable<SmartTriggerDto> GetList(Guid botId, string include = "")
        {
            IEnumerable<SmartTrigger> list = _domainService.GetbyBotId(botId, include);
            return Mapper.Map<IEnumerable<SmartTriggerDto>>(list);
        }
    }
}
